package com.mrc.ak.showMan.utils;

import java.awt.image.BufferedImage;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

/**
 * <p>
 * Description: 压缩图片的工具类
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2016-4-23 上午10:29:18          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ImgCompressUtil {
    //略缩图尺寸
    private static final int SMALL_SIZE = 30;
    private static final int BIG_SIZE = 600;

    /**
     * 压缩图片大小
     * @param sourcePath
     * @param toPic
     */
    public static void compress(String sourcePath, String toPic) {
        try {
            Thumbnails.of(sourcePath).scale(1f).outputQuality(0.25f).toFile(toPic);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 大或者小 的略缩图
     * @param sourcePath
     * @param boolSmall
     * @return
     */
    public static BufferedImage BufferedImagetoInputStream(String sourcePath, Boolean boolSmall, Integer width) {
        if (boolSmall == null)
            return BufferedImagetoInputStream(sourcePath, width, 1000);
        if (boolSmall)
            return BufferedImagetoInputStream(sourcePath, BIG_SIZE, SMALL_SIZE);
        return BufferedImagetoInputStream(sourcePath, BIG_SIZE, BIG_SIZE);
    }

    /**
     * 如果比例是空 按大小缩放，否则按照比例
     * 若图片横比width小，高比height小，不变   
     * 若图片横比width小，高比height大，高缩小到height，图片比例不变   
     * 若图片横比width大，高比height小，横缩小到width，图片比例不变   
     * 若图片横比width大，高比height大，图片按比例缩小，横为width或高为height   
     * 根据图片源路径 压缩成略缩图 
     * @param sourcePath 源图片地址
     * @param width 目标宽度
     * @param height 目标高度
     * @return   返回BufferedImage
     * @throws IOException
     */
    public static BufferedImage BufferedImagetoInputStream(String sourcePath, int width, int height) {
        BufferedImage image = null;
        try {
            image = Thumbnails.of(sourcePath).size(width, height).asBufferedImage();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 裁剪图片，并设置大小
     * @param sourcePath
     * @param x1
     * @param y1
     * @param w
     * @param h
     * @return
     */
    public static BufferedImage region(String sourcePath, int x1, int y1, int w, int h) {
        BufferedImage image = null;
        //目标大小480
        final int tagetSize = 480;
        try {
            //如果截取的大小小于目标大小 计算比例进行放大
            if (w < tagetSize) {
                float scale = Float.parseFloat(tagetSize + "") / w;
                image = Thumbnails.of(sourcePath).sourceRegion(x1, y1, w, h).scale(scale).asBufferedImage();
            } else {
                //截取 并缩放到目标大小
                image = Thumbnails.of(sourcePath).sourceRegion(x1, y1, w, h).size(tagetSize, tagetSize).asBufferedImage();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

}
